#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <minigui/common.h>
#include <minigui/minigui.h>
#include <minigui/gdi.h>
#include <minigui/window.h>
#include <minigui/control.h>
#include "resource.h"
#include "func.h"
#include "lang.h"
#include "com.h"
#include "mhlabel.h"
#include "mhedit.h"
#include "mhstatic.h"
#include "keypane.h"
#include "password.h"

/*
 *
 */
void SetAskInfo(HWND hWnd, const char *info)
{
    HWND hWndTmp = GetDlgItem(hWnd, IDC_TIP);
    if(hWndTmp)
    {
        SetWindowCaption(hWndTmp, info);
        InvalidateRect(hWndTmp, NULL, TRUE);
    }
}
/*
 *
 */
static void OnBtnCmd(HWND hWnd, ushort nID)
{
    switch(nID)
    {
    case ID_ESC:
        if(GetDlgItem(hWnd, nID))
        {
            PostMessage(hWnd, MSG_CLOSE, 0, 0);
        }
        break;
    case ID_ENTER:
        if(GetDlgItem(hWnd, nID))
        {
            SetWindowAdditionalData(hWnd, TRUE);
            PostMessage(hWnd, MSG_CLOSE, 0, 0);
        }
        break;
    }
}
/*
 *
 */
static void OnKeyDown(HWND hWnd, uint key)
{
    switch(key)
    {
    case SCANCODE_ESCAPE:
        OnBtnCmd(hWnd, ID_ESC);
        break;
    case SCANCODE_ENTER:
        OnBtnCmd(hWnd, ID_ENTER);
        break;
    }
}
/*
 *
 */
static int AskDlgProc(HWND hWnd, int message, WPARAM wParam, LPARAM lParam)
{
    switch(message)
    {
    case MSG_CREATE:
        {
            CreateWindow(MH_LABEL_NAME,
                "",
                WS_CHILD | WS_VISIBLE,
                IDC_TIP,
                20,
                60,
                CX_ASK_DLG_BMP - 40,
                20,
                hWnd, MH_LABEL_BKGND_3GREEN | MH_LABEL_TEXT_GREEN);
            CreateWindow(MH_STATIC_NAME,
                "",
                WS_CHILD | WS_VISIBLE,
                ID_ENTER,
                40,
                96,
                CX_YES_BMP,
                CY_YES_BMP,
                hWnd, (int)&g_bmp[BMP_YES]);
            CreateWindow(MH_STATIC_NAME,
                "",
                WS_CHILD | WS_VISIBLE,
                ID_ESC,
                CX_ASK_DLG_BMP - 40 - CX_NO_BMP,
                96,
                CX_NO_BMP,
                CY_NO_BMP,
                hWnd, (int)&g_bmp[BMP_NO]);
        }
        break;
    case MSG_PAINT:
        {
            HDC hdc = GetClientDC(hWnd);
            RECT rect;
            GetClientRect(hWnd, &rect);
            rect.top += MASTER_TITLE_TOP;
            rect.bottom = rect.top + MASTER_TITLE_HEIGHT - 1;
            SetBkMode(hdc, BM_TRANSPARENT);
            SetTextColor(hdc, COLOR_HQ_RED(hdc));
            DrawText(hdc, GetWindowCaption (hWnd), -1, &rect, DT_CENTER | DT_VCENTER | DT_SINGLELINE);
            ReleaseDC(hdc);
        }
        break;
    case MSG_CLOSE:
        {
            DestroyMainWindow(hWnd);
            MainWindowCleanup(hWnd);
        }
        return 0;
    case MSG_DESTROY:
        {
            ShowWindow(hWnd, SW_HIDE);
            DestroyAllControls(hWnd);
        }
        break;
    case MSG_KEYDOWN:
        {
            OnKeyDown(hWnd, wParam);
        }
        return 0;
    case MSG_ERASEBKGND:
        {
            DrawBitmapBkGnd(hWnd, (RECT*)lParam, &g_bmp[BMP_ASK_DLG]);
        }
        return 0;
    case MSG_COMMAND:
        {
            OnBtnCmd(hWnd, LOWORD(wParam));
        }
        return 0;
    }
    return DefaultMainWinProc(hWnd, message, wParam, lParam);
}
/*
 *
 */
HWND CreateAskDialog(HWND hHosting)
{
    int x = (CX_SCREEN - CX_ASK_DLG_BMP) / 2;
    int y = (CY_SCREEN - CY_ASK_DLG_BMP) / 2;
    MAINWINCREATE CreateInfo;
    CreateInfo.dwStyle = WS_VISIBLE;
    CreateInfo.dwExStyle = WS_EX_NONE;
    CreateInfo.spCaption = MW_ASK_TITLE_CAPTION;
    CreateInfo.hMenu = 0;
    CreateInfo.hCursor = GetSystemCursor(0);
    CreateInfo.hIcon = 0;
    CreateInfo.MainWindowProc = AskDlgProc;
    CreateInfo.lx = x;
    CreateInfo.ty = y;
    CreateInfo.rx = x + CX_ASK_DLG_BMP;
    CreateInfo.by = y + CY_ASK_DLG_BMP;
    CreateInfo.iBkColor = COLOR_lightwhite;
    CreateInfo.dwAddData = 0;
    CreateInfo.hHosting = hHosting;
    HWND hWnd = CreateMainWindow(&CreateInfo);
    if(hWnd != HWND_INVALID)
    {
        SetFocusChild(GetDlgItem(hWnd, ID_ESC));
    }
    return hWnd;
}
/*
 *
 */
int AskDoModal(HWND hWnd)
{
    int ret = 0;
    MSG Msg;
    HWND hHosting = GetHosting(hWnd);
    if(hHosting > 0)
    {
        EnableWindow(hHosting, FALSE);
    }
    if(hWnd != HWND_INVALID)
    {
        ShowWindow(hWnd, SW_SHOWNORMAL);
        while(GetMessage(&Msg, hWnd))
        {
            if(Msg.hwnd == hWnd && Msg.message == MSG_CLOSE)
            {
                ret = GetWindowAdditionalData(hWnd);
                break;
            }
            TranslateMessage(&Msg);
            DispatchMessage(&Msg);
        }
        if(hHosting > 0)
        {
            EnableWindow(hHosting, TRUE);
            ShowWindow(hHosting, SW_SHOWNORMAL);
            SetActiveWindow(hHosting);
            SendMessage(hWnd, MSG_CLOSE, 0, 0);
        }
    }
    return ret;
}
/*
 *
 */
int AfxMessageBox(HWND hWnd, const char *title, const char *sc)
{
    HWND hDlg = CreateAskDialog(hWnd);
    if(title)
    {
        SetWindowCaption(hDlg, title);
    }
    if(sc)
    {
        SetAskInfo(hDlg, sc);
    }
    return AskDoModal(hDlg);
}

